const app = getApp();
import {phoneList,departList } from "../../../config/api";
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    limit:1000000,
    isEmpty:false,
    departList:[],//部门列表
    phoneList:[],//电话列表
    flag:false,//默认不弹出下拉菜单
    active:100000,//默认激活全部按钮
    type:null,//部门类别
    key:'',//搜索关键字
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.getDepartList();
     this.getPhoneList(null);
  },

 //获取部门电话列表
 getPhoneList(){
   let that = this;
   let params = {
    page:1,
    limit:10000000,
   };
   if(that.data.key!=''){
    params = {
      page:1,
      limit:10000000,
      name:that.data.key
     };
   }
   util.apiRequest(phoneList,params,null).then(r=>{
     console.log('获取的电话列表为：',r.data.data);
     that.setData({
      phoneList:r.data.data
     });
      if(r.data.data.length==0){
        that.setData({
          isEmpty:true
        });
      }else{
        that.setData({
          isEmpty:false
        });
      }
   });
 },

  //获取部门列表
  getDepartList(){
    let that = this;
    util.apiRequest(departList,null,null).then(r=>{
      console.log('部门列表数据为：',r.data.data);
      that.setData({
        departList:r.data.data
      });
    });
       
  },

  //拨打电话
  dial(e){
    let that = this;
    console.log('e',e.currentTarget.dataset.phone);
    wx.makePhoneCall({
     phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
   })
  },

  //通过关键字进行搜索
  searchBykey(e){
    let that = this;
    console.log('输入框输入的值为：',e.detail.value);
    that.setData({
      key:e.detail.value
    });
    let params = {
      page:1,
      limit: 10000000,
      name:e.detail.value 
    };
    if(that.data.type){
      params = {
        page:1,
        limit: 10000000,
        type:that.data.type,
        name:e.detail.value 
      };
    }
    util.apiRequest(phoneList,params,null).then(r=>{
         console.log('搜索的值为：',r.data.data);
         that.setData({
           phoneList:r.data.data
         });
         if(r.data.data.length==0){
           that.setData({
             isEmpty:true
           });
         }else{
          that.setData({
            isEmpty:false
          });
         }
    });
  },

  //折叠面板
  collapse(){
    let that = this;
    console.log('折叠菜单');
    that.setData({
       flag:!that.data.flag
    });
  },
  //tab选项卡切换
  change(e){
    let that = this;
    let item = e.currentTarget.dataset.item;
    let index= e.currentTarget.dataset.index;
    that.setData({
      active:index,
    });
    if(item==0){
      that.getPhoneList();
      that.setData({
        type:null,
        flag:false,
        active:100000,
      });
      return;
    }
    let params = {
      page:1,
      limit:10000000,
      type:item.id,
      name:that.data.key
    };
    that.setData({
      type:item.id
    });

    util.apiRequest(phoneList,params,null).then(r=>{
      console.log('搜索的值为：',r.data.data);
      that.setData({
        phoneList:r.data.data
      });
      if(r.data.data.length==0){
        that.setData({
          isEmpty:true
        });
      }else{
       that.setData({
         isEmpty:false
       });
      }
      that.setData({
        flag:false
      });
 
    });
    


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})